//
//  ZGuessController.m
//  NewProject
//
//  Created by 陈洪鑫 on 2018/4/13.
//  Copyright © 2018年 chenIos. All rights reserved.
//

#import "ZChangeWalletController.h"
#import "ZChangeWalletListCell.h"

//#import <Foundation/Foundation.h>
@interface ZChangeWalletController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong) NSArray *setItems;

@property (nonatomic,strong) NSMutableArray *wallets;

@property(nonatomic,assign)NSInteger  page;
@end

@implementation ZChangeWalletController
- (NSMutableArray *)wallets
{
    if (!_wallets) {
        _wallets = [NSMutableArray array];
    }return _wallets;
}
- (NSArray *)setItems
{
    if (!_setItems) {
        _setItems = @[Z_Location(@"transactionLog"),Z_Location(@"setting"),Z_Location(@"support"),Z_Location(@"about")];
    }return _setItems;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        CGFloat navHeight = iPhoneX ? 88 + 34 : 64;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - navHeight) collectionViewLayout:[UICollectionViewFlowLayout new]];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:@"ZChangeWalletListCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ZChangeWalletListCell"];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }return _collectionView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = Z_Location(@"switchAccount");
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self getWalletsItem];
}

- (void)getWalletsItem{
    [ZDBManager queryWallets:0 completion:^(NSArray *wallets) {
        NSArray *items = [ZWalletAccountInfo mj_objectArrayWithKeyValuesArray:wallets];
        [self.wallets addObjectsFromArray:items];
    }];
}
- (void)changeWalletAction{
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.wallets.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZChangeWalletListCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"ZChangeWalletListCell" forIndexPath:indexPath];
    ZWalletAccountInfo *item = self.wallets[indexPath.section];
    
    if (item.isSelected) {
        cell.selectedBtn.hidden = NO;
    }else
    {
        cell.selectedBtn.hidden = YES;
    }
  
    
   
    cell.titlelabel.text = item.name;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
    
}


#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    for (ZWalletAccountInfo *item in self.wallets) {
//        item.isSelected = NO;
//    }
    ZWalletAccountInfo *item =self.wallets[indexPath.section];
    
    
    item.isSelected = YES;
    if (self.ChangeWallet) {
        self.ChangeWallet(item);
    }
//    self.collectionView real
    [self.navigationController popViewControllerAnimated:YES];
    
    
//    [ZDBManager changeSelectedWallet:item.walletAdress completion:^(BOOL isSuccess) {
//
//    }];
////    [ZWalletAccount wallet].selectedWallet = item;
////    [[ZWalletAccount wallet] writeToFile];
//    [self.collectionView reloadData];
    
    
    
    
}

#pragma mark -- UICollectionViewDelegateFlowLayout


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = SCREEN_W;
    //    CGFloat height = 180;
    
    return CGSizeMake(width, 50);
    
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsZero;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;



@end
